package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.model.Feedback;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class FeedBackActivity extends BaseActivity {


    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.rl_toolbar)
    RelativeLayout rlToolbar;
    @InjectView(R.id.edt_feedback)
    EditText edtFeedback;
    @InjectView(R.id.rl_input)
    RelativeLayout rlInput;
    @InjectView(R.id.btn_commit)
    Button btnCommit;


    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在上传");
        progressDialog.setCancelable(false);
        ButterKnife.inject(this);
        initEvent();
    }

    private void initEvent() {

        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String feedbackContent = edtFeedback.getText().toString().trim();
                if (edtFeedback.getText() == null || feedbackContent.equals("")) {
                    ToastUtils.showToast(getApplicationContext(), "请输入您的意见");
                    return;
                } else {
                    progressDialog.show();
                    Feedback feedback = new Feedback();
                    feedback.setContent(feedbackContent);
                    feedback.setUser(jianzhijiaApplacation.getCompanyUser().getAvUser());
                    feedback.getFeedback().saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            progressDialog.dismiss();
                            if (e == null) {
                                ToastUtils.showToast(getApplicationContext(), "上传成功");
                                finish();
                            } else {
                                ToastUtils.showToast(getApplicationContext(), "上传失败 " + ExceptionUtils.getExceptionInfo(e));
                            }
                        }
                    });
                }
            }
        });
    }
}
